package com.example.fang.demodruid.concurrency5;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class MyTest5 {
    
    public static void main(String[] args){
/*        String result= CompletableFuture.supplyAsync(()->"hello").thenApplyAsync(value->value+"world").join();
        System.out.println(result);
        System.out.println("=========================");
        CompletableFuture.supplyAsync(()->"hello").thenAccept(value->System.out.println("welcome"+value));
        System.out.println("=========================");*/
        //对于stage的合并操作
        System.out.println("主线程开始执行");
        String result2=CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(5000);
            }catch (Exception e){
                e.printStackTrace();
            }
            return "hello";
        }).thenCombine(CompletableFuture.supplyAsync(()->{
            try {
                Thread.sleep(5000);
            }catch (Exception e){
                e.printStackTrace();
            }
            return "world";
        }),(s1,s2)->s1+" "+s2).join();
        System.out.println(result2);
        System.out.println("主线程执行完成");
        Runnable r1=()->{
            System.out.println(result2);
        };
        Thread t1=new Thread(r1);
        t1.start();
/*        CompletableFuture<Void> completableFuture=CompletableFuture.runAsync(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(2000);
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("task finished");
        });
        //不会阻塞主线程的执行
        completableFuture.whenComplete((t,action)->System.out.println("执行完成"));
        System.out.println("主线程执行完毕");
        try {
            //为了子线程能够打印出来
            TimeUnit.MILLISECONDS.sleep(7000);
        }catch (Exception e){
            e.printStackTrace();
        }*/

    }
}
